package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Tag;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("tag")
public class TagController {

    @Autowired
    TagService tagService;

    //查询标签
    @GetMapping("/list")
    public ResponseResult<PageInfo<Tag>> listTag(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                 @RequestParam(value = "size", defaultValue = "2") Integer size,
                                                 String name) {
        ResponseResult<PageInfo<Tag>> result = new ResponseResult<>();
        try {
            PageInfo<Tag> pageInfo = tagService.queryTagByName(now, size, name);
            if (pageInfo != null) {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            } else {
                result.setCode(201);
                result.setMsg("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }

    //修改标签名称
    @PutMapping("/modify")
    public ResponseResult<Void> modifyTag(@RequestBody Tag tag) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            tagService.modifyTag(tag);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }

    //添加标签
    @PostMapping("/add")
    public ResponseResult<Void> addTag(@RequestBody Tag tag) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            tagService.addTag(tag);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return result;
    }

    //删除标签
    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> delTag(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            tagService.delTag(id);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return result;
    }

    // 初始化标签，添加资源时用到
    @GetMapping("/initAllTag")
    public ResponseResult<List<Tag>> initAllTag() {
        ResponseResult<List<Tag>> result = new ResponseResult<>();
        try {
            List<Tag> list = this.tagService.queryAll();
            result.setMsg("查询标签成功!");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询标签失败!");
            result.setCode(201);
        }
        return result;
    }
}
